package cc.jethro.jdxyq.activity;


import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.design.widget.BottomNavigationView;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.view.MenuItem;

import cc.jethro.jdxyq.R;
import cc.jethro.jdxyq.fragment.CommunityFragment;
import cc.jethro.jdxyq.fragment.HomeFragment;
import cc.jethro.jdxyq.fragment.MeFragment;
import cc.jethro.jdxyq.fragment.NotificationsFragment;


public class MainActivity extends BaseActivity {

    private BottomNavigationView bottomNavigationView ;
    private FragmentManager fm ;

    private HomeFragment homeFragment ;
    private CommunityFragment communityFragment ;
    private NotificationsFragment notificationsFragment ;
    private MeFragment meFragment ;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        initView();
        initNavigation();

        //添加默认要显示的fragment
        homeFragment = new HomeFragment();
        fm = getSupportFragmentManager();
        FragmentTransaction fragmentTransaction = fm.beginTransaction();
        fragmentTransaction.replace(R.id.content_view, homeFragment);
        fragmentTransaction.commit();


    }


    /**
     * 初始化VIEW
     */
    protected void initView() {
        bottomNavigationView = (BottomNavigationView) findViewById(R.id.navigation_view);
    }

    /**
     * 初始化navigation
     */
    protected void initNavigation() {

         // 监听底部导航栏的选择

        bottomNavigationView.setOnNavigationItemSelectedListener(
                new BottomNavigationView.OnNavigationItemSelectedListener() {
                    @Override
                    public boolean onNavigationItemSelected(@NonNull MenuItem item) {
                        FragmentTransaction fragmentTransaction = fm.beginTransaction();
                        switch (item.getItemId()) {

                            //主页选项
                            case R.id.navigation_home:
                                hideFragment(meFragment,fragmentTransaction);
                                hideFragment(notificationsFragment,fragmentTransaction);
                                hideFragment(communityFragment,fragmentTransaction);
                                //判断该fragment是否为空，为空就创建一个新的，然后add
                                //不为空就直接show
                                if (homeFragment == null) {
                                    homeFragment = new HomeFragment();
                                    fragmentTransaction.add(R.id.content_view,homeFragment);
                                } else {
                                    fragmentTransaction.show(homeFragment);
                                }
                                break;

                            //社区选项
                            case R.id.navigation_community:
                                hideFragment(homeFragment,fragmentTransaction);
                                hideFragment(notificationsFragment,fragmentTransaction);
                                hideFragment(meFragment,fragmentTransaction);
                                if (communityFragment == null) {
                                    communityFragment = new CommunityFragment();
                                    fragmentTransaction.add(R.id.content_view,communityFragment);
                                } else {
                                    fragmentTransaction.show(communityFragment);
                                }
                                break;
                            //消息选项
                            case R.id.navigation_notifications:
                                hideFragment(homeFragment,fragmentTransaction);
                                hideFragment(communityFragment,fragmentTransaction);
                                hideFragment(meFragment,fragmentTransaction);
                                if (notificationsFragment == null) {
                                    notificationsFragment = new NotificationsFragment();
                                    fragmentTransaction.add(R.id.content_view,notificationsFragment);
                                } else {
                                    fragmentTransaction.show(notificationsFragment);
                                }
                                break;
                            //个人选项
                            case R.id.navigation_me:
                                hideFragment(homeFragment,fragmentTransaction);
                                hideFragment(communityFragment,fragmentTransaction);
                                hideFragment(notificationsFragment,fragmentTransaction);
                                if (meFragment == null) {
                                    meFragment = new MeFragment();
                                    fragmentTransaction.add(R.id.content_view,meFragment);
                                } else {
                                    fragmentTransaction.show(meFragment);
                                }
                                break;
                        }

                        fragmentTransaction.commit();
                        return true;
                    }
                });

    }

    /**
     * 隐藏Fragment
     * @param fragment 要显示的fragment
     * @param fragmentTransaction
     */
    private void hideFragment(Fragment fragment, FragmentTransaction fragmentTransaction) {
        if (fragment != null) {
            fragmentTransaction.hide(fragment);
        }
    }

}
